<template>
  <div class="equip-repair-part-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>
        <el-button :icon="DocumentAdd" plain type="primary" @click="openAdd">
          新增
        </el-button>
        <el-button
          :disabled="!currentRef"
          :icon="Edit"
          plain
          type="warning"
          @click="openUpdate"
        >
          修改
        </el-button>
        <el-button
          :disabled="!currentRef"
          :icon="Delete"
          plain
          type="danger"
          @click="deleteDialog"
        >
          删除
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :cell-style="{ padding: '6px 0px' }"
      :data="parts"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="部位编码" prop="partCode" width="180" />
      <el-table-column label="部位名称" prop="partName" width="180" />
      <el-table-column label="设备编码" prop="equipCode" width="180" />
      <el-table-column label="设备名称" prop="equipName" width="180" />
      <el-table-column label="创建人" prop="createUser" width="120" />
      <el-table-column label="创建时间" prop="createTime" width="200" />
      <el-table-column label="修改人" prop="createUser" width="120" />
      <el-table-column label="修改时间" prop="createTime" width="200" />
      <el-table-column label="备注" prop="remark" width="220" />
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <addRepairPart ref="addRepairPartRef" @success-callback="addCallback" />
    <queryPart ref="queryPartRef" @success-callback="queryCallback" />
    <updatePart ref="updatePartRef" @success-callback="updateCallback" />
    <deletePart ref="deletePartRef" @success-callback="deleteCallback" />
  </div>
</template>

<script setup>
  import { Search, DocumentAdd, Edit, Delete } from '@element-plus/icons-vue'
  import addRepairPart from './modules/addRepairPart.vue'
  import { localStorageConfig } from '@/config/localstorage.config'
  import { getRepairPartApi } from '@/api/equip/equip_api'
  import queryPart from './modules/queryPart.vue'
  import updatePart from './modules/updatePart.vue'
  import deletePart from './modules/deletePart.vue'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const queryForm = ref({ pkCorp: pkCorp })

  const updatePartRef = ref()
  const deletePartRef = ref()

  const parts = ref()
  const queryPartRef = ref()
  const addRepairPartRef = ref()
  const currentRef = ref()

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 20,
    total: 0,
  })

  const getParts = async () => {
    const load = $baseLoading()
    try {
      let params = {
        ...queryForm.value,
        ...paginationParams,
      }
      const { code, msg, data } = await getRepairPartApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        parts.value = data.list
        paginationParams.total = data.total
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const openQueryDialog = () => queryPartRef.value.openDialog()

  const queryCallback = (value) => {
    queryForm.value = value
    getParts()
  }

  const handleCurrentChange = (rowData) => (currentRef.value = rowData)

  const openAdd = () => addRepairPartRef.value.openDialog()

  const addCallback = () => getParts()

  const openUpdate = () => updatePartRef.value.openDialog(currentRef.value)

  const deleteDialog = () => deletePartRef.value.openDialog(currentRef.value)

  const deleteCallback = () => getParts()

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
  }
  //#endregion

  watch(paginationParams, () => getParts())

  onMounted(() => getParts())
</script>
